


use "$data/DAILY.PANEL.dta", clear

keep if date<=21977

merge m:1 iso2c year month day using "$data/COVID.DAILY.dta"
keep if _merge==1 | _merge==3
drop _merge

merge m:1 iso2c using "$data/first_hth_transmission_NOMISSING.dta"
keep if _merge==1 | _merge==3
drop _merge

replace date_first_hth = 0 if date_first_hth==.

gen postfirsthth = date>=date_first_hth 


merge m:1 iso2c year month day using "$data/GOOGLE.TRENDS.DAILY.dta"
keep if _merge==1 | _merge==3
drop _merge



capture label drop google
label define google 1 "recession_hits" 2  "stockmarket_crash_hits" 3 "conspiracy_theory_hits" 4   "prepper_hits"
global googlesearchvar = "recession_hits  stockmarket_crash_hits conspiracy_theory_hits  prepper_hits"



encode iso2c , gen(iso)
sort iso date


xtset iso date

format date %td


destring $googlesearchvar, replace force

sort iso date

foreach var in covid_confirmed covid_death covid_recovered {

replace `var' = 0 if  `var'==. 

}


foreach var in covid_confirmed covid_death covid_recovered {

gen any`var' = `var'>0 if `var'!=. 

}

label variable anycovid_confirmed "First confirmed COVID19 case"
label variable anycovid_death "First confirmed COVID19 death"


gen temp = date if anycovid_confirmed ==1
egen mintemp = min(temp) , by(iso )
egen firstcorona = min(temp) , by(iso )
gen timetofirst = date-firstcorona 
drop temp


replace firstcorona = 51977 if firstcorona ==.

foreach var in $googlesearchvar  {

egen temp = mean(`var') if date<firstcorona, by(iso2c)

egen pre`var' = mean(temp), by(iso2c)
drop temp

}

foreach var in $googlesearchvar {

replace `var' = `var'/(pre`var'+1)

}


keep if year==2020
eststo clear
 
 
loc experiments "recession_hits  stockmarket_crash_hits conspiracy_theory_hits  prepper_hits"
		   
			   
preserve

clear all
eststo clear
estimates drop _all

set obs 10
qui gen x = 1
qui gen y = 1

loc columns = 0

foreach choice in `experiments' {

    loc ++columns
    qui eststo col`columns': reg x y

}

restore


/* Statistics */

loc colnum = 1
loc colnames ""

foreach choice in `experiments' {

/* Statistics */

reghdfe  `choice'   anycovid_confirmed      , absorb(iso date) vce(cl iso )
 
    sigstar anycovid_confirmed , prec(3)
    estadd loc thisstat3 = "`r(bstar)'": col`colnum'
    estadd loc thisstat4 = "`r(sestar)'": col`colnum'
	
	estadd loc thisstat6 = string(`e(r2)', "%9.2f"): col`colnum'
	qui sum `choice'  
	estadd loc thisstat7 =  r(N): col`colnum'
	

reghdfe  `choice'   postfirsthth     , absorb(iso date) vce(cl iso )
 
    sigstar postfirsthth , prec(3)
    estadd loc thisstat10 = "`r(bstar)'": col`colnum'
    estadd loc thisstat11 = "`r(sestar)'": col`colnum'
	
	estadd loc thisstat13 = string(`e(r2)', "%9.2f"): col`colnum'
	qui sum `choice'  
	estadd loc thisstat14 =  r(N): col`colnum'
	estadd loc thisstat15 = "`e(N_clust)'" : col`colnum'
	estadd loc thisstat16 =  "X": col`colnum'
	estadd loc thisstat17 =  "X": col`colnum'

	
    loc ++colnum
    loc colnames "`colnames' `"`: var la `choice''"'"

	


	} 
	

	

loc rowlabels " "{\bf Panel  A: Any Covid-19 case}" " " "Post any Covid-19 case" " "  " " "\midrule R$^2$" "Number of Observations " "\midrule {\bf Panel B: Any human-to-human transmission}" " " "Post any human-to-human transmission" " " " " "\midrule R$^2$" "Number of Observations" " \midrule Number of countries" "Country FE" "Day FE" "
loc rowstats ""

forval i = 1/16 {
    loc rowstats "`rowstats' thisstat`i'"
}

esttab  using "$output/tables/Main_DD_results.tex", replace cells(none) booktabs nonotes  compress alignment(c) nogap noobs nobaselevels label stats(`rowstats', labels(`rowlabels')) ///
  mtitle("Recession" "Stock Market Crash" "Conspiracy Theory"   "Survivalism"  ) /// 
   mgroups("Impact on Goolge search trends",  pattern(1 0 0  0  ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))


eststo  clear


*** Output coefficients for graph

foreach  var in anycovid_confirmed  postfirsthth  {
preserve
keep if year==2020
local nlen :  word count   $googlesearchvar

matrix hrid = J(`nlen',6,.)
estimates clear
global iter =1
foreach depvar in  $googlesearchvar { 

reghdfe  `depvar'   `var'      , absorb(iso date) vce(cl iso )
 matrix hrid[$iter,1] = $iter
lincom `var'
 matrix hrid[$iter,3] = `r(estimate)'
 matrix hrid[$iter,2] = `r(estimate)' - 1.95 * `r(se)'
 matrix hrid[$iter,4] = `r(estimate)' + 1.95 * `r(se)'
 matrix hrid[$iter,5] = `r(se)' 

su `depvar' 
 matrix hrid[$iter,6] = `r(mean)' 
 	 
global iter  = $iter +1
}
svmat hrid
label values hrid1 google
keep hrid*
drop if hrid1==. 
rename hrid1 dv
rename hrid2 lowerci
rename hrid3 est
rename hrid4 upperci
rename hrid5 se
rename hrid6 meandv

outsheet using "$output/tables/tables_raw/coefs`var'.csv", replace   comma    
restore
}

